<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/4/12
 * Time: 下午8:21
 */

namespace app\admin\model;


class AuthRule extends BaseModel
{

    //  主菜单节点
    const RULE_MAIN = 1;
    //  侧边栏菜单
    const RULE_SIDEBAR = 2;
    //  普通按钮节点
    const RULE_URL = 3;


    /**
     * @var bool 自动写入创建和更新的时间戳字段
     */
    protected $autoWriteTimestamp = false;

    /**
     * 更新规则信息表
     * @param array $data
     * @param $url
     * @param $type
     * @return bool
     */
    public function saveRule(array $data, $url, $type)
    {
        $info = $this->where('name', $url)->where('type', $type)->find();

        if ($info) {
            foreach ($data as $key=>$value) {
                $info->$key = $value;
            }
            $re = $this->allowField(true)->where('id', $info->id)->update($data);
        } else {
            $data['condition'] = '';
            $re = $this->allowField(true)->insert($data);
        }
        if ($re !== false) {
            return true;
        }
        return false;
    }

    /**
     * 根据rule name 和 type  查找编号
     * @param $name
     * @param $type
     * @return mixed
     */
    public function getRuleIdByNameType($name, $type)
    {
        return $this->where('name', $name)->where('type', $type)->value('id');
    }
}